<template>
  <div>
    <div class="beijing">
    <el-row style="height: 840px;">
      <search-bar @onSearch="searchResult" ref="searchBar"></search-bar>
      <el-tooltip effect="dark" placement="right"
                  v-for="item in books.slice((currentPage-1)*pagesize,currentPage*pagesize)"
                  :key="item.id">
        <p slot="content" style="font-size: 14px;margin-bottom: 6px;">{{item.title}}</p>
        <p slot="content" style="font-size: 13px;margin-bottom: 6px">
          <span>{{item.author}}</span> /
          <span>{{item.date}}</span> /
          <span>{{item.press}}</span>
        </p>
        <p slot="content" style="width: 300px" class="abstract">{{item.abs}}</p>
        <el-card style="width: 135px;margin-bottom: 40px;height: 300px;float: left;margin-right: 15px" class="book"
                 bodyStyle="padding:10px" shadow="hover">
          <div class="cover"  >
            <img :src="item.cover" alt="封面" @click="clickcover(item)">
            
          </div>
          <div class="info">
            <div class="title">
              <a href="">{{item.title}}</a>
            </div> 
            <!-- <i class="el-icon-picture-outline"  @click="reface(item.id)"></i>
            <i class="el-icon-delete-solid" @click="deleteBook(item.id)"></i>
            <i class="el-icon-edit-outline" @click="editBook(item)"></i> -->
          </div>
         <div class="author">{{item.author}}</div>
           <el-button type="primary" round @click="buy(item)">购买</el-button>
          
        </el-card>
  
          
       
        
      </el-tooltip>
      <!-- <edit-form @onSubmit="loadBooks()" ref="edit"></edit-form> -->
      <information  ref="inf"></information>
    </el-row>
    <el-row>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pagesize"
        :total="books.length">
      </el-pagination>
    </el-row>
    <reface ref="ref"></reface>
  </div>
  </div>
</template>

<script>
  import EditForm from '../houtai/EditForm.vue'
import SearchBar from './SearchBar'
  import Reface from './Reface.vue'
import Information from './Information.vue'

  export default {
    name: 'Books',
    components: { EditForm, SearchBar, Reface, Information },
    data () {
      return {
        books: [],
        currentPage: 1,
        pagesize: 17
      }
    },
    //当mounted生命阶段时加载图书
    mounted: function () {
      this.loadBooks()
    },
  methods: {
      // 通过发送http://localhost:8443/api/books路径
    loadBooks() {
        console.log(6666);
        var _this = this
        this.$axios.get('/books').then(resp => {
          if (resp && resp.status === 200) {
            _this.books = resp.data
          }
        })
      },
      handleCurrentChange: function (currentPage) {
        this.currentPage = currentPage
        console.log(this.currentPage)
      },
      searchResult () {
        this.$axios.get('/search?keyword=' + this.$refs.searchBar.keywords)
          .then((resp) => {
            if (resp && resp.status === 200) {
              if (resp.data == '') {
                this.loadBooks()
              } else {
                this.books = resp.data
              }
          
        }
       })
         
      },
    //   deleteBook (id) {
    //     this.$confirm('此操作将永久删除该书籍, 是否继续?', '提示', {
    //       confirmButtonText: '确定',
    //       cancelButtonText: '取消',
    //       type: 'warning'
    //     }).then(() => {
    //         this.$axios
    //           .post('/delete', {id: id}).then(resp => {
    //           if (resp && resp.status === 200) {
    //             this.loadBooks(),
    //               this.opendelete()
    //           }
    //         })
    //       }
    //     ).catch(() => {
    //       this.$message({
    //         type: 'info',
    //         message: '已取消删除'
    //       })
    //     })
    //     // alert(id)
    // },
    // reface(id) {
    //   this.$refs.ref.id=id
    //   this.$refs.ref.dialogVisible = true
    //   console.log(55555555);
        
    //   },
    //   editBook (item) {
    //     this.$refs.edit.dialogFormVisible = true
       
    //       this.$refs.edit.form = {
    //       id: item.id,
    //       cover: item.cover,
    //       title: item.title,
    //       author: item.author,
    //       date: item.date,
    //       press: item.press,
    //       abs: item.abs,
    //       category: {
    //         id: item.category.id.toString(),
    //         name: item.category.name
    //       }
        
    //     }
        
    // },
    // opendelete() {
    //   this.$message({
    //     message: '删除成功',
    //      type:'success'

    //   })
    // },
    clickcover(item) {
      this.$refs.inf.dialogVisible = true
      this.$refs.inf.bookname = item.title
      this.$refs.inf.author = item.author
      this.$refs.inf.abs = item.abs
      this.$refs.inf.cover = item.cover
      this.$refs.inf.date = item.date
      this.$refs.inf.press = item.press
      console.log(item);
      // this.$router.replace({ path: "/information" })
    },
    buy(book)
    {
      console.log(typeof(book.title)); 
      var data = new FormData()
      
      data.append('book', book.title)
      this.$axios.post('/buy', data)
        .then(success => {
          if (success.data.code === 200) {
            this.$message({
        message: '购买成功',
         type:'success'

      })
            }
        }  )
      
    }
   
    }
  }
</script>
<style scoped>

  .cover {
    width: 115px;
    height: 172px;
    margin-bottom: 7px;
    overflow: hidden;
    cursor: pointer;
  }

  img {
    width: 115px;
    height: 172px;
    /*margin: 0 auto;*/
  }

  .title {
    font-size: 14px;
    text-align: left;
    
  }

  .author {
    color: #333;
    width: 102px;
    font-size: 13px;
    margin-bottom: 10px;
    margin-left: 5px;
    text-align: center;
  }

  .abstract {
    display: block;
    line-height: 17px;
  }

  .el-icon-delete-solid {
    cursor: pointer;
    float: right;
  }
  .el-icon-edit-outline{
    cursor: pointer;
    float: left;
  }


  .switch {
    display: flex;
    position: absolute;
    left: 780px;
    top: 25px;
  }

  a {
    text-decoration: none;
  }

  a:link, a:visited, a:focus {
    color: #3377aa;
  }
  .beijing{
    width: 100%;
    height: 860px;
    background-image: url('../../assets/shudianbeijing.jpg');
    background-size: cover;
}

</style>

